package satellite

import "sort"

type TransportSlice []Transport

func (p TransportSlice) Len() int           { return len(p) }
func (p TransportSlice) Less(i, j int) bool { return p[i].Type() < p[j].Type() }
func (p TransportSlice) Swap(i, j int)      { p[i], p[j] = p[j], p[i] }
func (p TransportSlice) Sort()              { sort.Sort(p) }
func (l TransportSlice) Append(t Transport) TransportSlice {
	ll := append(l, t)
	ll.Sort()
	return ll
}
func (l TransportSlice) Remove(index int) TransportSlice {
	var ll TransportSlice
	switch index {
	case 0:
		ll = l[1:]
	case len(l) - 1:
		ll = l[0:index]
	default:
		ll = append(l[:index], l[index+1:]...)
	}
	ll.Sort()
	return ll
}

func (l TransportSlice) Search(t Transport) int {
	index := sort.Search(l.Len(), func(i int) bool { return l[i].Type() >= t.Type() })
	if index < l.Len() && l[index].Type() == t.Type() {
		return index
	} else {
		return -1
	}
}

type TransportTypeSlice []TransportType

func (p TransportTypeSlice) Len() int           { return len(p) }
func (p TransportTypeSlice) Less(i, j int) bool { return p[i] < p[j] }
func (p TransportTypeSlice) Swap(i, j int)      { p[i], p[j] = p[j], p[i] }
func (p TransportTypeSlice) Sort()              { sort.Sort(p) }

func (l TransportTypeSlice) Search(t TransportType) int {
	index := sort.Search(l.Len(), func(i int) bool { return l[i] >= t })
	if index < l.Len() && l[index] == t {
		return index
	} else {
		return -1
	}
}
